﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mouseControl : MonoBehaviour {

	public float JetPower = 275.0f;//火箭的喷力
	private Rigidbody2D rig;//刚体组件
	private bool isActive=false;//火箭是否激活/是否点击鼠标
    public float FlySpeed = 3.0f;

    public LayerMask Ground;
    private Transform GroundCheckPoint;//脚本检测点

    public ParticleSystem MyParticle;
    //public bool isGrounded;

    public AudioClip CollectAudio;//吃金币的音效
    public AudioClip LasertAudio;//激光音效
    private AudioSource FootStepAudio;//脚步音效
    public AudioSource JetAudio;//火箭音效

    [HideInInspector]
    public int Count = 0;//金币数目
    public bool isDead=false;
    private Animator anim;
    // Use this for initialization
    void Start () {
		rig=GetComponent<Rigidbody2D>();
        GroundCheckPoint=transform.Find("GroundCheck");
        FootStepAudio=GetComponent<AudioSource>();
        Debug.Log(FootStepAudio);
        anim=GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
        isActive = Input.GetMouseButton(0) || (
                  Input.touchCount > 0 && (
                  Input.GetTouch(0).phase == TouchPhase.Began ||
                  Input.GetTouch(0).phase == TouchPhase.Stationary
                  )
                  );

        //isActive = Input.GetMouseButton(0) || (
        //    Input.touchCount > 0 && (
        //      Input.GetTouch(0).phase == TouchPhase.Began ||
        //      Input.GetTouch(0).phase == TouchPhase.Stationary
        //    )
        //    );
        //判断老鼠是否着地
        bool isGrounded = Physics2D.OverlapCircle(
         GroundCheckPoint.transform.position,
         0.1f, Ground);
        //切换状态
        if (anim!=null)
        {
            anim.SetBool("ground",isGrounded);
            anim.SetBool("dead",isDead);
        }
        //Vector2 pos = new Vector2(
        //    GroundCheckPoint.position.x,
        //    GroundCheckPoint.position.y);


        //if (Physics2D.OverlapCircle(pos,0.1f,Ground))
        //{
        //    Debug.Log("地上");
        //}
        //else
        //{
        //    Debug.Log("天上");
        //}


        //粒子处理系统
        ParticleSystem.EmissionModule em = MyParticle.emission;
        em.enabled = !isGrounded;
        em.rateOverTime = isActive ? 300.0f : 75.0f;
        
        //播放脚步
        //FootStepAudio.mute = !isGrounded;
        //FootStepAudio.Play();
        FootStepAudio.enabled=isGrounded;


        //if (Physics2D.OverlapCircle(
        //    GroundCheckPoint.transform.position,
        //    0.1f, Ground))

        //{
        //    Debug.Log("地上");
        //    //MyParticle.emission.enabled = false;//错误写法


        //}
        //else
        //{
        //    Debug.Log("天上");

        //}
    }
    
	private void FixedUpdate()//引擎里的都放这里
	{
		
		//Debug.Log("True");
		
		if (rig!=null)
		{
           if (isActive&&rig)
		   {
              rig.AddForce(JetPower * Vector2.up);
		   }
            rig.velocity = new Vector2(FlySpeed, rig.velocity.y);
        }
    }
    //用于刚体/碰撞体
    private void OnTriggerEnter2D(Collider2D collision)//刚撞上
    {
		//Debug.Log("刚撞上");
        //Debug.Log(collision.name);
        if (collision.CompareTag("MyCoin"))
        {
            Debug.Log("碰到金币了...");
            //只是隐藏
            //collision.gameObject.SetActive(false);//把碰上的物体隐藏起来
            //销毁金币
            AudioSource.PlayClipAtPoint(CollectAudio,transform.position);
             Destroy(collision.gameObject);//销毁碰到的游戏物体
            Count++;

            //延时销毁
            //Destroy(collision.gameObject,1);
        }
        if (collision.CompareTag("Harm"))
        {
            Debug.Log("老鼠被干掉了...");
            isDead = true;
            FlySpeed = 0;
            AudioSource.PlayClipAtPoint(LasertAudio, transform.position);
            
        }
    }
    private void AfterDeath()
    {

    }
    //private void OnTriggerStay2D(Collider2D collision)//停在上面
    //{
    //    Debug.Log("穿透中....");
    //}
    //private void OnTriggerExit2D(Collider2D collision)//离开接触
    //{
    //    Debug.Log("离开了");
    //}
    //用于触发器
    //private void OnCollisionEnter2D(Collision2D collision)
    //{

    //    Debug.Log("碰上");
    //}
    //private void OnCollisionStay2D(Collision2D collision)
    //{
    //    Debug.Log("碰撞中.....");
    //}

    //private void OnCollisionExit2D(Collision2D collision)
    //{
    //    Debug.Log("没有碰");
    //}
    
}
